package com.android.bandroid.guide.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.bandroid.R;
import com.android.bandroid.guide.GuideActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2015/11/14.
 */
public class GuideAdapter extends PagerAdapter {
    private List<ImageView> mImageViews = new ArrayList<ImageView>();
    private int[] IMGS;
    private Context mContext;
    private ImageView imageView;
    private LayoutInflater inflater;
    private GuideActivity.GuideCallBackListener guideCallBackListener;

    public GuideAdapter(int[] imgs, Context mContext, GuideActivity.GuideCallBackListener guideCallBackListener) {
        this.IMGS = imgs;
        this.mContext = mContext;
        this.guideCallBackListener = guideCallBackListener;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        System.gc();
        if(position == IMGS.length-1){
            View view = inflater.inflate(R.layout.guide_end, null);
            view.setBackgroundResource(IMGS[position]);
            imageView = (ImageView) view.findViewById(R.id.img_guide);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //  立即体验跳转
                    guideCallBackListener.doGuideFinish();
                }
            });
            container.addView(view);
            return view;
        }else{
            imageView = new ImageView(mContext);
            imageView.setImageResource(IMGS[position]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            container.addView(imageView);
            mImageViews.add(imageView);
            return imageView;
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(mImageViews.get(position));
        System.gc();
    }

    @Override
    public int getCount() {
        return IMGS.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
